package com.vcc.msc.sbs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

public class JavaScriptInterface {
	Context mContext;

	/** Instantiate the interface and set the context */
	JavaScriptInterface(Context c) {
		mContext = c;
	}

	/** Show a toast from the web page */
	public void showToast(String toast) {
		Toast.makeText(mContext, "Balance group : " + toast, Toast.LENGTH_SHORT)
				.show();
	}

	public void openBalanceNewWindow(String balanceId) {
		Dialog d;
		d = new Dialog(mContext, android.R.style.Theme_InputMethod);
		d.setContentView(R.layout.open_balance_row);

		WebView webview = (WebView) d.findViewById(R.id.webView_balanceOpen);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setBuiltInZoomControls(false);
		webview.addJavascriptInterface(new JavaScriptInterface(mContext),
				"Android");

		webview.loadUrl("file:///android_asset/open_balance.html");

		d.show();
		WindowManager.LayoutParams lp = d.getWindow().getAttributes();
		lp.dimAmount = 0.7f;
		d.getWindow().setAttributes(lp);
		d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}

	public void showBalanceOptions(final String balanceId) {
		final CharSequence[] items = { "Open balance", "Show current car" };// TODO
																			// Enum

		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				if (item == 0) {
					// Open balance
					Dialog d;
					d = new Dialog(mContext, android.R.style.Theme_InputMethod);
					d.setContentView(R.layout.open_balance_graph);

					TextView balanceDetailsView = (TextView) d
							.findViewById(R.id.textView_balanceDetails);

					balanceDetailsView.setText("Balance Id - " + balanceId);

					WebView webview = (WebView) d
							.findViewById(R.id.webView_balanceGrapgOpen);
					webview.getSettings().setJavaScriptEnabled(false);
					webview.getSettings().setBuiltInZoomControls(true);

					webview.loadUrl("file:///android_asset/balanceGraph1311106.png");

					d.show();
					WindowManager.LayoutParams lp = d.getWindow()
							.getAttributes();
					lp.dimAmount = 0.7f;
					d.getWindow().setAttributes(lp);
					d.getWindow().addFlags(
							WindowManager.LayoutParams.FLAG_DIM_BEHIND);

				} else {
					// Show current car
					Toast.makeText(
							mContext,
							"TODO! Current car details for balance : "
									+ balanceId, Toast.LENGTH_SHORT).show();
				}

			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

}
